package com.master.train.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.master.train.business.dao.TrainCarriageDao;
import com.master.train.business.entity.TrainCarriageEntity;
import com.master.train.business.service.TrainCarriageService;
import com.master.train.common.utils.PageUtils;
import com.master.train.common.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("trainCarriageService")
public class TrainCarriageServiceImpl extends ServiceImpl<TrainCarriageDao, TrainCarriageEntity> implements TrainCarriageService {

    @Autowired
    TrainCarriageDao trainCarriageDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TrainCarriageEntity> page = this.page(
                new Query<TrainCarriageEntity>().getPage(params),
                new QueryWrapper<TrainCarriageEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<TrainCarriageEntity> selectByTrainCode(String trainCode) {
        // 1. 使用 LambdaQueryWrapper，通过实体类方法引用字段（避免硬编码字符串）
        LambdaQueryWrapper<TrainCarriageEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainCarriageEntity::getTrainCode, trainCode);  // 重点：用 Lambda 引用字段

        // 2. 执行查询
        List<TrainCarriageEntity> list = trainCarriageDao.selectList(queryWrapper);

        // 3. 打印调试信息（确认参数和结果）
        System.out.println("查询参数 trainCode: " + trainCode);
        System.out.println("查询结果数量: " + list.size());

        // 4. 空校验
        if (list.isEmpty()) {
            throw new RuntimeException("车次 " + trainCode + " 下不存在车厢");
        }
        return list;
    }

}